package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.graduatetest.R;
import com.example.graduatetest.adapter.NoticeAdapter;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.bean.User;
import com.example.graduatetest.callback.NoticeListCallBack;
import com.example.graduatetest.databinding.ActivityNoticeListBinding;
import com.example.graduatetest.model.NoticeModel;
import com.example.graduatetest.utils.Constant;

import java.util.List;

public class NoticeListViewModel extends ViewModel {
    private Activity activity;
    private ActivityNoticeListBinding binding;
    private String type;
    private Classes ownClass;
    private List<Notice> mNoticeList;
    private Handler handler = new Handler();
    private User user;
    private boolean isLeave;

    public void init(Activity activity,ActivityNoticeListBinding binding,Classes ownClass,String type){
        this.activity = activity;
        this.binding = binding;
        this.ownClass = ownClass;
        this.type = type;
        user = Constant.checkStudentOrTeacher(activity);
        toolbarInit();
        initView();
        getData();
    }

    private void initView(){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        isLeave = preferences.getBoolean("isLeave", false);
    }

    //初始化toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar;
        toolbar.setTitle(type+"列表");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = activity.getApplication().getSharedPreferences("data", Context.MODE_PRIVATE).edit();
                editor.putBoolean("isLeave", false);
                editor.apply();
                activity.onBackPressed();
            }
        });
    }

    //初始化recyclerView
    private void recyclerInit(){
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity.getApplication());
        binding.recyclerViewNotice.setLayoutManager(layoutManager);
        if(mNoticeList != null){
            NoticeAdapter noticeAdapter = new NoticeAdapter(mNoticeList,activity);
            binding.recyclerViewNotice.setAdapter(noticeAdapter);
        }

    }

    private void getData(){
        if(!type.equals("请假") || isLeave){
            NoticeModel.selectNoticeListByUserAndType(user.getNumber(), type,ownClass.getId(), new NoticeListCallBack() {
                @Override
                public void onSuccess(List<Notice> noticeList) {
                    mNoticeList = noticeList;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            recyclerInit();
                        }
                    });
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity,"数据获取失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }else{
            NoticeModel.selectNoticeListByUserAndType(user.getNumber(), type, new NoticeListCallBack() {
                @Override
                public void onSuccess(List<Notice> noticeList) {
                    mNoticeList = noticeList;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            recyclerInit();
                        }
                    });
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity,"数据获取失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }
    }
}
